/**
 *  fairy 版权所有 Copyright@2016年3月18日
 */
package com.fairy.core.encrypt;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
/**
 * [AES.java]
 *
 * @ProjectName: [fairy_core]
 * @Author: [waysun]
 * @CreateDate: [2016年3月18日 下午3:33:53]
 * @Update: [说明本次修改内容] BY [waysun] [2016年3月18日 下午3:33:53]
 * @Version: [v1.0]
 */

public class AES 
{  
	// KeyGenerator 提供对称密钥生成器的功能，支持各种算法
	  private static KeyGenerator keygen;
	  // SecretKey 负责保存对称密钥
	  private static SecretKey deskey;
	  // Cipher负责完成加密或解密工作
	  private static Cipher c;
	  // 该字节数组负责保存加密的结果
	  private static byte[] cipherByte;
	  
	  //private static String key="";

	  static {
	    //Security.addProvider(new com.sun.crypto.provider.SunJCE());
	    // 实例化支持DES算法的密钥生成器(算法名称命名需按规定，否则抛出异常)
	    try {
	      keygen = KeyGenerator.getInstance("AES");
	      
	    } catch (NoSuchAlgorithmException e) {
	      // TODO Auto-generated catch block
	      e.printStackTrace();
	    }
	    // 生成密钥
	    
	    // 生成Cipher对象,指定其支持的AES算法
	    try {
	      c = Cipher.getInstance("AES");
	    } catch (NoSuchAlgorithmException e) {
	      // TODO Auto-generated catch block
	      e.printStackTrace();
	    } catch (NoSuchPaddingException e) {
	      // TODO Auto-generated catch block
	      e.printStackTrace();
	    }
	  }


	  /**
	   * 对字符串加密
	   * 
	   * @param str
	   * @return
	   * @throws InvalidKeyException
	   * @throws IllegalBlockSizeException
	   * @throws BadPaddingException
	   */
	  
	  public static String Encrytor(String str,String key) throws InvalidKeyException,
	      IllegalBlockSizeException, BadPaddingException {
		  keygen.init(128,new SecureRandom(key.getBytes()));
		    //keygen.init(128, new SecureRandom(key.getBytes()));
		    deskey = keygen.generateKey();
		    
	    // 根据密钥，对Cipher对象进行初始化，ENCRYPT_MODE表示加密模式
	    c.init(Cipher.ENCRYPT_MODE, deskey);
	    byte[] src = str.getBytes();
	    // 加密，结果保存进cipherByte
	    cipherByte = c.doFinal(src);
	    return Byte2Hex.byte2Hex(cipherByte);
	  }

	  /**
	   * 对字符串解密
	   * 
	   * @param buff
	   * @return
	   * @throws InvalidKeyException
	   * @throws IllegalBlockSizeException
	   * @throws BadPaddingException
	   */
	  public static String Decryptor(String buff,String key) throws InvalidKeyException,
	      IllegalBlockSizeException, BadPaddingException {
		  keygen.init(128,new SecureRandom(key.getBytes()));
		    //keygen.init(128, new SecureRandom(key.getBytes()));
		    deskey = keygen.generateKey();
	    // 根据密钥，对Cipher对象进行初始化，DECRYPT_MODE表示加密模式
	    c.init(Cipher.DECRYPT_MODE, deskey);
	    cipherByte = c.doFinal(Byte2Hex.hex2Byte(buff));
	    return new String(cipherByte);
	  }




	  
	   /**
	   * @param args
	   * @throws NoSuchPaddingException
	   * @throws NoSuchAlgorithmException
	   * @throws BadPaddingException
	   * @throws IllegalBlockSizeException
	   * @throws InvalidKeyException
	   */
	   public static void main(String[] args) throws Exception {
		   //AES de1 = new AES();
		   String msg ="lp;l;l;jmhj";
		   String key="33333ddd";
		   String encontent = Encrytor(msg,key);
		   String decontent = Decryptor(encontent,key);
		   System.out.println("明文是:" + msg);
		   System.out.println("加密后:" + encontent);
		   System.out.println("解密后:" + decontent);
	   }
}  



